﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WLMCoreLibrary;
using MiWebSite_Core;

namespace MWS_Blog
{
    public partial class _Default : System.Web.UI.Page
    {
        WebSiteMainConfiguration config = WebSiteMainConfiguration.GetConfig();
        dbConnector ConexionDB;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["xml"] != null)
            {
                GetPageXML();
            }
            else
                TransformXML(GeneratePage());

        }

        private MiWebSitePage GeneratePage()
        {
            ConexionDB = new dbConnector(config.Connections[0].value);
            MiWebSitePage pagina = new MiWebSitePage();
            try
            {
                pagina = new MiWebSitePage(ref ConexionDB);
                MiWebSiteFunctions.LoadIncludesWebSite(pagina);
                if (CurrentUser.IdUsuario != 0)
                {
                    MiWebSiteFunctions.LoadIncludesWebSiteAdmin(pagina);
                    BlogFunctions.LoadIncludesAdmin(pagina);
                }
                MiWebSiteBlogEntryCollection listaBlog;
                if (Request["Tag"] != null)
                {
                    listaBlog = new MiWebSiteBlogEntryCollection(Request["Tag"].ToString(), 1, true, ref ConexionDB);
                    pagina.Title = Request["Tag"] + " - " + pagina.Title;
                }
                else
                    if (Request["Categoria"] != null)
                        listaBlog = new MiWebSiteBlogEntryCollection(Request["Categoria"].ToString(), 1, false, ref ConexionDB);
                    else
                        listaBlog = new MiWebSiteBlogEntryCollection(ref ConexionDB);
                pagina.Contenido = WLMFunctions.SerializeObject(listaBlog);

            }
            catch (Exception ex)
            {

            }
            ConexionDB.CloseConnection();
            return pagina;
        }

        private void TransformXML(MiWebSitePage pagina)
        {
            List<ParametroTransformacionXML> parametros = new List<ParametroTransformacionXML>();
            parametros.Add(new ParametroTransformacionXML("isMobile", MiWebSiteFunctions.isMobileBrowser().ToString().ToLower()));
            parametros.Add(new ParametroTransformacionXML("skinPath", config.Settings["skinPath"].value));            
            string HTMLPagina = WLMFunctions.TransformXML(pagina, config.Settings["siteURL"].value + config.Settings["skinPath"].value + "index.xsl", parametros);
            Response.Write(HTMLPagina);
            Response.End();
        }

        private void GetPageXML()
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            Response.HeaderEncoding = System.Text.Encoding.UTF8;
            string xmlportada = WLMFunctions.SerializeObject(GeneratePage(), true);
            Response.Write(xmlportada);
            Response.End();
        }


    }
}